﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.EarTage;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.EarsCommands
{
    public class LambEarCreateHander : IRequestHandler<LambEarCreateCommand, Result>
    {

        private readonly ILogger<LambEarCreateHander> logger;
        private readonly IBaseRepository<LambEars> lambEarRepository;

        public LambEarCreateHander(ILogger<LambEarCreateHander> logger, IBaseRepository<LambEars> lambEarRepository)
        {
            this.logger = logger;
            this.lambEarRepository = lambEarRepository;
        }

        /// <summary>
        /// 添加产羔耳号信息
        /// </summary>
        /// <param name="request">添加产羔耳号信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>添加产羔耳号信息相应对象</returns>
        public async Task<Result> Handle(LambEarCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                //判断单添还是批量添加
                if (!string.IsNullOrEmpty(request.EarNumber))
                {
                    var info = lambEarRepository.GetAll().FirstOrDefault(c => c.EarNumber == request.EarNumber&&!c.Deletes);
                    if (info != null)
                    {
                        result.Code =  -1;
                        result.Messege = "该耳号已经存在";
                        return result;
                    }
                    else
                    {
                        LambEars lambEars = new LambEars();
                        lambEars.EarNumber = request.EarNumber;
                        lambEars.PutInDate = request.PutInDate;
                        lambEars.BrithdayStatusId = request.BrithdayStatusId;
                        lambEars.BreedId = request.BreedId;
                        lambEars.UseStatus = false;
                        lambEars.CreateId =request.CreatId;
                        lambEars.CreateTime = DateTime.Now;
                        result.Code = lambEarRepository.Add(lambEars);
                        result.Messege = "产羔耳号信息添加成功";
                    }
                }
                else
                {
                    int n = request.GetNumber;
                    for (int i = 1; i <= n; i++)
                    {
                        LambEars lambEars = new LambEars();
                        lambEars.EarNumber = request.Prefix + request.FieldNumber.ToString(); ;
                        var info = lambEarRepository.GetAll().FirstOrDefault(c => c.EarNumber == lambEars.EarNumber && !c.Deletes);
                        if (info != null)
                        {
                            result.Code = -1;
                            result.Messege = "耳号为" + lambEars.EarNumber + "的产羔已经存在，已生成" + (i - 1) + "条产羔耳标数据，往后数据停止生成";
                            return result;
                        }
                        lambEars.PutInDate = request.PutInDate;
                        lambEars.BrithdayStatusId = request.BrithdayStatusId;
                        lambEars.BreedId = request.BreedId;
                        lambEars.UseStatus = false;
                        lambEars.CreateId = request.CreatId;
                        lambEars.CreateTime = DateTime.Now;
                        result.Code += lambEarRepository.Add(lambEars);
                        request.FieldNumber++;
                    }
                    result.Messege = n + "条产羔耳标数据添加成功";
                }
                logger.LogInformation("添加产羔耳标信息方法调用成功");
                return result;
            }
            catch (Exception ex )
            {
                logger.LogError("添加产羔耳标信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
